package com.siruier.boss.ui.activity.user

import android.app.Activity
import android.view.View
import android.widget.RadioGroup
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.loginApi
import com.siruier.boss.databinding.ActivityRealNameAuthBinding
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.cache.UserCache
import com.siruier.boss.ui.ext.toastMessageLong
import com.siruier.boss.ui.ext.value
import com.siruier.boss.ui.tool.image.select.domain.ImageSelectBean
import com.siruier.boss.ui.tool.image.select.imageSelect
import com.siruier.boss.ui.tool.image.select.service.intercept.ApiUploadFileIntercept
import com.siruier.boss.ui.utils.GlideUtils
import com.siruier.boss.ui.utils.variableView

class RealNameAuthActivity : BaseActivity<ActivityRealNameAuthBinding>() {
    private var idCardFrontImg: ImageSelectBean? = null
    private var idCardBackImg: ImageSelectBean? = null
    private var licenseUrlImg: ImageSelectBean? = null
    private var shopUrlImg: ImageSelectBean? = null
    override fun initView() {
        bindViewClick(vb.llIdCardFrontImg, vb.llIdCardBackImg, vb.llLicenseUrl, vb.llShopUrl, vb.tbGetCode, vb.buttonSave)
        UserCache.getUserInfoBean().apply {
            if (authStatus == 1) {
                vb.titleView.titleStr = "修改银行卡号"
                vb.buttonSave.text = "修改"
                vb.llShop.isGone = true
            }
            vb.llName.isVisible = cardOwner.isEmpty()
            vb.etName.setText(cardOwner)
            vb.llIdCardNo.isVisible = idCardNo.isEmpty()
            vb.etIdCardNo.setText(idCardNo)

            vb.etPhone.setText(cardPhone)
        }
        vb.rgAuthModel.setOnCheckedChangeListener(::authModel)
        authModel()
        variableView {
            addTextView(vb.etName)
            addTextView(vb.etIdCardNo)
            addTextView(vb.etPhone)
            addTextView(vb.etCode)
            addTextView(vb.etBankName)
            addTextView(vb.etBankNo)
            addOnValidationResultListener(vb.buttonSave)
            addOnValidationInterceptListener(::validator)
        }
    }

    private fun validator(view: View) = when (view) {
        vb.etPhone -> vb.tbGetCode.enable(vb.etPhone.length() == 11)
        vb.etCode -> vb.etCode.length() == 4
        vb.etIdCardNo -> vb.etIdCardNo.length() == 18
        else -> null
    }

    private fun authModel(group: RadioGroup? = null, checkedId: Int? = null) {
        vb.llShop.isVisible = vb.rbShop.isChecked
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.llIdCardFrontImg -> imageSelect.selectImage {
                isMultipleChoice = false
                onUploadSuccessListener = {
                    idCardFrontImg = it?.get(0)
                    GlideUtils.intoView(mThis, idCardFrontImg?.path, vb.ivCardFrontImg)
                }
            }
            vb.llIdCardBackImg -> imageSelect.selectImage {
                isMultipleChoice = false
                onUploadSuccessListener = {
                    idCardBackImg = it?.get(0)
                    GlideUtils.intoView(mThis, idCardBackImg?.path, vb.ivCardBackImg)
                }
            }
            vb.llLicenseUrl -> imageSelect.selectImage {
                isMultipleChoice = false
                onUploadSuccessListener = {
                    licenseUrlImg = it?.get(0)
                    GlideUtils.intoView(mThis, licenseUrlImg?.path, vb.ivLicenseUrl)
                }
            }
            vb.llShopUrl -> imageSelect.selectImage {
                isMultipleChoice = false
                onUploadSuccessListener = {
                    shopUrlImg = it?.get(0)
                    GlideUtils.intoView(mThis, shopUrlImg?.path, vb.ivShopUrl)
                }
            }
            vb.tbGetCode -> {
                launchUI({ loginApi.smsSend(vb.etPhone.value, "idAuth").toData() }, ApiObserver(this) {
                    vb.tbGetCode.start()
                })
            }
            vb.buttonSave -> {
                if (idCardFrontImg == null) {
                    "请选择身份证正面".toastMessageLong()
                    return
                }
                if (idCardBackImg == null) {
                    "请选择身份证反面".toastMessageLong()
                    return
                }
                if (vb.llShop.isVisible) {
                    if (licenseUrlImg == null) {
                        "请选择营业执照".toastMessageLong()
                        return
                    }
                    if (shopUrlImg == null) {
                        "请选择门头照".toastMessageLong()
                        return
                    }
                }
                launchUI({
                    ApiUploadFileIntercept.uploadFile(
                        "idCard", mutableListOf(idCardFrontImg, idCardBackImg)
                            .filterNotNullTo(mutableListOf())
                    )
                    loginApi.idCardAuth(
                        vb.etName.value,
                        vb.etIdCardNo.value,
                        vb.etPhone.value,
                        vb.etCode.value,
                        vb.etBankName.value,
                        vb.etBankNo.value,
                        idCardFrontImg?.url,
                        idCardBackImg?.url,
                        licenseUrlImg?.url,
                        shopUrlImg?.url,
                        if (vb.rbUser.isChecked) 1 else 2
                    ).toData()
                    UserCache.saveUserInfoBean(loginApi.userInfo().toData())
                }, ApiObserver(this, cancelable = false, commonButton = vb.buttonSave) {
                    "实名认证成功".toastMessageLong()
                    setResult(Activity.RESULT_OK)
                    finish()
                })
            }
        }
    }
}